package com.useage.day06.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.useage.day06.model.User;

@Repository
public class UserDao {
	
	// spring提供的jdbc连接对象, 自动注入进来
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int deleteUser(Integer uId) {
		String sql = "delete t_user where u_id=?";
		
		Object[] args = {uId};
		
		int n = jdbcTemplate.update(sql, args);

		System.out.println(n);
		
		return n;
	}
	
	
	
	// 
	public int updateUser(User newUser) {
		String sql = "update t_user set u_name=? , u_password=? , u_mobile=? , r_id=? where u_id=?";
		
		Object[] args = {newUser.getU_name(), newUser.getU_password(), newUser.getU_mobile(), newUser.getR_id(), newUser.getU_id()};
		
		int n = jdbcTemplate.update(sql, args);

		System.out.println(n);
		
		return n;
	}
	
	
	
	public User getUserById(Integer uId) {
		
		String sql = "select * from t_user where u_id=?";
		
		Object[] args = {uId};
		
		List<User> list = jdbcTemplate.query(sql, args, new BeanPropertyRowMapper(User.class));

		if (list.size() < 1) {
			return null;
		}
		
		return list.get(0);
	}
	
	
	
	// 查询所有用户
	public List getAllUser() {
		
		// System.out.println(jdbcTemplate);
		
		String sql = "SELECT * FROM t_user";
		
		List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class));
		
		System.out.println(list);
		
		
		return null;
	}

	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
}
